﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;
using System.IO;

public partial class new_edit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bd();
        }
    }
    void bd()
    {
        int id = Convert.ToInt32(Request["id"]);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.News news = dal.News.First(x => x.id == id);
        txtNewsId.Text = news.id.ToString();
        txtTitle.Text = news.title;
        txtContent.Text = news.content;
        txtKeyWords.Text = news.keyword;
        txtStock.Text = news.Stock;
        txtAuthor.Text = news.author;
        txtSummary.Text = news.Summary;
        txtSource.Text = news.Source;
        int tid =int.Parse( Request["tid"]);
        ddlType.DataTextField = "Title";
        ddlType.DataValueField = "Id";
        ddlType.DataSource = dal.Topics2Title.Where(x => x.Tid == tid).OrderBy(x=>x.index);
        ddlType.SelectedValue = Request["titleId"];
        ddlType.DataBind();
        int titleId=int.Parse( Request["titleId"]);
        var tNews= dal.Topics2News.First(x => x.NewsId == id && x.TitleId == titleId);
        Image1.ImageUrl="~/file/"+ tNews.ImgUrl;
        ViewState["filename"] = tNews.ImgUrl;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request["id"]);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.News news = dal.News.First(x => x.id == id);
        news.author = txtAuthor.Text;
        news.content = txtContent.Text;
        news.keyword = txtKeyWords.Text;
        news.title = txtTitle.Text;
        news.Summary = txtSummary.Text;
        news.Stock = txtStock.Text;
        news.Source = txtSource.Text;
        news.step = int.Parse(ddlStep.SelectedValue);
        news.ctime = news.ctime;
        try
        {
            //删除相关股票
            var stocks = dal.NewsRelatedStocks.Where(x => x.NewsId == news.id);
            foreach (var c in stocks)
            {
                dal.NewsRelatedStocks.DeleteObject(c);
            }

            //添加相关股票
            if (!string.IsNullOrEmpty(txtStock.Text))
            {
                string[] cc = txtStock.Text.Split(',', '，');
                foreach (var c in cc)
                {
                    NewsRelatedStocks s = new NewsRelatedStocks();
                    s.NewsId = news.id;
                    s.Stock = c;
                    try
                    {
                        s.StockName = CommonBll.DicStocks[s.Stock];
                    }
                    catch
                    {
                    }
                    dal.NewsRelatedStocks.AddObject(s);
                }
            }
            int yTitleId=int.Parse( Request["titleId"]);
            var tNews= dal.Topics2News.First(x => x.TitleId == yTitleId && x.NewsId == id);
            tNews.TitleId = int.Parse(ddlType.SelectedValue);
            try
            {
                tNews.ImgUrl = ViewState["filename"].ToString();
            }
            catch
            {
            }
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='news.aspx?tid="+Request["tid"]+"'", true);
        }
        catch (Exception ex)
        {
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "EditNews", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改失败')", true);
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string pathFolder = Server.MapPath("~/file/topics/" + Request["tid"] + "/");
        string fileName = FileUpload1.FileName;
        string houzui = "";
        if (fileName.IndexOf(".") > -1)
        {
            houzui = fileName.Substring(fileName.IndexOf("."));
        }
        string guid = Guid.NewGuid().ToString();
        ViewState["filename"] = "/topics/" + Request["tid"] + "/" + guid + houzui;
        string rName = pathFolder + guid + houzui;
        FileInfo file = new FileInfo(rName);
        if (!file.Directory.Exists)
        {
            file.Directory.Create();
        }
        FileUpload1.SaveAs(rName);
        Image1.ImageUrl = "~/file/topics/" + Request["tid"] + "/" + guid + houzui;
    }
}